/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.bean;

import com.jb.faces.el.AttributeEvaluator;
import com.jb.web.context.WebContext;

import com.jb.faces.component.UIComponent;

import javax.servlet.jsp.el.ELException;

public class FormField extends ValueHolderImpl {
    static final long serialVersionUID = 123123L;

    static {
        publish(FormField.class, GENERAL, "enabled");
        publish(FormField.class, GENERAL, "accessKey");
        publish(FormField.class, GENERAL, "fieldName");

        publish(FormField.class, DATA, "value");
        publish(FormField.class, STYLE, "hidden");
        
        publish(FormField.class, EVENT_HANDLER, "onValidate");

        publish(FormField.class, JS_HANDLER, "onChange");

    }

    private String accessKey;
    private String fieldName;
    private String label;
    private String onChange;
    private boolean enabled = true;
    private boolean hidden;
    private boolean mandatory;
    private boolean showLabel = true;
    private String valueFormat;

    /**
     * Creates a new FormItem object.
     */
    public FormField() {
        super();
    }

    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }

    public String getAccessKey() {
        return accessKey;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }

    public String getFieldName() {
        return (fieldName == null) ? getClientId() : fieldName;
    }

    public String getFieldName(WebContext pCtx) {
        try {
            return (String)AttributeEvaluator.evaluate((WebContext) pCtx, this, getFieldName());
        } catch (ELException e) {
            return getFieldName();
        }
    }

    public Form getForm() {
        UIComponent parentForm = getParent();

        if (parentForm == null) {
            return null;
        }

        while (!(parentForm instanceof Form)) {
            parentForm = parentForm.getParent();

            if (parentForm == null) {
                break;
            }
        }

        return (Form) parentForm;
    }

    public void setHidden(boolean hidden) {
        this.hidden = hidden;
    }

    public boolean isHidden() {
        return hidden;
    }

    public void setLabel(String pLabel) {
        label = pLabel;
    }

    public String getLabel() {
        return label;
    }

    public void setLabelVisible(boolean pShowLabel) {
        this.showLabel = pShowLabel;
    }

    public boolean getLabelVisible() {
        return showLabel;
    }

    public void setMandatory(boolean newValue) {
        mandatory = newValue;
    }

    public boolean getMandatory() {
        return mandatory;
    }
    public void setModelBean(Object modelBean) {
        super.setModelBean(modelBean);
        setValue(modelBean==null?null:modelBean.toString());
    }
    
    public Object getModelBean() {
        Object object = super.getModelBean();
        if (object == null) {
            object = this.getDecodedValue();
        }
        return object;
    }
    /**
     * Client on change event handler
     * @param onChange
     */
    public void setOnChange(String onChange) {
        this.onChange = onChange;
    }
    /**
     * Returns client on chagne event handler
     * @return client on change event handler
     */
    public String getOnChange() {
        return onChange;
    }

    public String toString() {
        return this.getValue();
    }

    public void setValueFormat(String param) {
        this.valueFormat = param;
    }

    public String getValueFormat() {
        return valueFormat;
    }
}
